package uia.alumni.profile;

import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.http.HttpServletRequest;
import uia.alumni.data.CvEntry;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.web.Page;



/**
 *
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class EditCvEntry   extends Page implements Constants {

    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "profile.editcventry";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;
    

    public EditCvEntry() {
        super(Role.member);
    }


    /**
     * Write a  form for editing a cv entry. The cv entry is
     * specified with the parameter CVENTRY. If it contains a valid cv
     * entry id, and THE CV ENTRY BELONGS TO THE USER, the from
     * is populated with data, otherwise an empty form for writing
     * a new cv entry will be written.
     * @param out
     * @param request
     */
    public void writeBody(PrintWriter out, HttpServletRequest request) {
        EntityManager em = emf.createEntityManager();
        User user = getLoggedInUser(em, request);
        long cveId = parseLong(request.getParameter(CVENTRY), -1);

        CvEntry cve = user.getCvEntry(cveId);
        if (cve == null) cve = new CvEntry();

        out.println("<h1>Editing "+user.getFullName()+"'s CV</h1>");
        printStartForm(out, SaveCvEntry.SERVLET_NAME);
//        out.println("<form action=\"cv.info\" method=\"post\">");
        out.println("<input type=\"hidden\" name=\""+CVENTRY+"\" value=\""+cveId+"\"/>");
        out.println("<table>");
        printDateSelector(out, "From", cve.getFromDate(), FROMDATE);
        printDateSelector(out, "To", cve.getToDate(), TODATE);
        printTextField(out, "Title", TITLE, cve.getType(), true);
        printTextArea(out, "Description", DESCRIPTION, cve.getDescription(), true);
        out.print("<tr><td><input type=\"submit\" value=\"Save\"/>");
        out.print("</td><td>");
        printLink(out, "[Cancel]", EditCv.SERVLET_NAME);
        out.println("</table>");
        out.println("</form>");
    }
    

    public final static long serialVersionUID = 1;

}
